#include "sem_demo.h"
UINT16 semCount = 0;
extern TSK_INIT_PARAM_S task1;
extern TSK_INIT_PARAM_S task2;

static VOID TaskF01(void)
{
    UINT32 ret;
    printf("\n+++++++++++++++++++++++TaskF01:(1) before sem pend success,check actual Count = %d,it should be 2\n",Count);
    ret = LOS_SemPend(g_semTest1,LOS_WAIT_FOREVER);
    Count++;
    //LOS_TaskDelay(50);
    ret = LOS_SemPost(g_semTest1);
    printf("\n+++++++++++++++++++++++TaskF01:(2) before sem post success, check actual Count = %d,it should be 3\n",Count);
    return;
}

static VOID TaskF02(void)
{
    UINT32 ret;

    Count++;
    printf("\n+++++++++++++++++++++++TaskF02:(1) before sem pend success, check actual Count = %d,it should be 1\n",Count);
    ret = LOS_SemPend(g_semTest1,LOS_WAIT_FOREVER);

    Count++;
    printf("\n+++++++++++++++++++++++TaskF02:(2) after sem pend success, check actual Count = %d,it should be 2\n",Count);
    printf("\n+++++++++++++++++++++++TaskF02:(3) continue hold sem for 50 ticks,TaskF01 should be scheduled\n");
    LOS_TaskDelay(50);
    printf("\n+++++++++++++++++++++++TaskF02:(4) sem post, TaskF01 will continue exe within the next 50 ticks\n");
    ret = LOS_SemPost(g_semTest1);
    LOS_TaskDelay(10);

    Count++;
    printf("\n+++++++++++++++++++++++TaskF02:(5) after sem post check actual Count = %d,it should be 4\n",Count);
}

VOID  Sem_Demo(VOID)
{
    UINT32 ret;

    ret = LOS_SemCreate(semCount,&g_semTest1);
    if(ret==LOS_OK){
        printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~This is liweili homeworks~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
        printf("\n+++++++++++++++++++Sem Demo:sem create ok with handle = 0x%x\n",Count);
    }

    LOS_TaskLock();

    task1.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskF01;
    task1.usTaskPrio = TASK_PRIO_TEST;
    task1.pcName = "VSemB2_1";
    task1.uwStackSize = 0x900;
    task1.uwResved = 0;

    ret = LOS_TaskCreate(&g_testTaskID01,&task1);
    if(ret==LOS_OK){
        printf("\n+++++++++++++++++++SemDemo:TaskF01 created ok with ID = 0x%x\n",g_testTaskID01);
    }

    task2.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskF02;
    task2.usTaskPrio = (TASK_PRIO_TEST-2);
    task2.pcName = "VSemB2_@";
    task2.uwStackSize = 0x900;
    task2.uwResved = 0;

    ret = LOS_TaskCreate(&g_testTaskID02,&task2);
    if(ret==LOS_OK){
        printf("\n+++++++++++++++++++SemDemo:TaskF02 created ok with ID = 0x%x\n",g_testTaskID02);
    }

    LOS_TaskUnlock();

    printf("\n++++++++++++++++++++Sem Demo:will delay 92 ticks\n");
    LOS_TaskDelay(90);

    printf("\n++++++++++++++++++++Sem Demo:after 90ticks, finally count = %d\n",Count);

    ret = LOS_SemDelete(g_semTest1);
    if(ret==LOS_OK){
        printf("\n++++++++++++++++++++Sem Demo:g_semTest1 deleted ok\n");
    }

    return;
}

